package leo.mystudy.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lichao email:lichao@witspring.com
 * @Description:
 * @Date 2018/3/28 11:13
 * @since Ver V
 */
@Slf4j
public class FileUtil {

    public static final String TABS = "\t";
    public static final String NEWLINE = "\n";

    public static BufferedWriter getBufferedWriter(String outPath) {
        File outFile = new File(outPath);
        OutputStreamWriter out;
        try {
            out = new OutputStreamWriter(new FileOutputStream(outFile));
            return new BufferedWriter(out);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            log.info("文件路径{}错误", outPath);
            return null;
        }
    }

    public static PrintWriter getPrintWriter(String outPath) {
        try {
            return new PrintWriter(new File(outPath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            log.info("文件路径{}错误", outPath);
            return null;
        }
    }

    public static void saveStringToFile(String text, String outPath) {
        BufferedWriter writer = getBufferedWriter(outPath);
        if (writer == null) {
            return;
        }
        try {
            writer.write(text);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
            log.info("保存文件异常,文件路径{}", outPath);
        }
    }

    public static BufferedReader getReader(File inFile) {
        try {
            InputStreamReader in = new InputStreamReader(new FileInputStream(inFile));
            return new BufferedReader(in);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            log.info("读取文件异常");
            return null;
        }
    }

    public static BufferedReader getReader(String inPath) {
        File file = new File(inPath);
        BufferedReader result = getReader(file);
        if (result == null) {
            log.info("读取文件异常,输入文件路径:{}", inPath);
        }
        return result;
    }

    public static List<String> getLines(String inPath) throws IOException {
        BufferedReader reader = getReader(inPath);
        if (reader == null) {
            return null;
        }
        List<String> list = new ArrayList<>();
        String line;
        while ((line = reader.readLine()) != null) {
            list.add(line);
        }
        reader.close();
        return list;
    }

    /**
     * 得到文件的第一行
     *
     * @param file
     * @return
     * @throws IOException
     */
    public static String getFirstLine(File file) throws IOException {
        BufferedReader reader = getReader(file);
        if (reader == null) {
            return null;
        }
        return reader.readLine();
    }

    /**
     * 得到文件目录下的文件列表
     *
     * @param inDirPath 输入文件目录
     * @return
     */
    public static File[] getFiles(String inDirPath) {
        File dir = new File(inDirPath);
        if (!dir.isDirectory()) {
            log.info("【输入文件目录地址错误】,inDirPath:{}", inDirPath);
            return null;
        }
        return dir.listFiles();
    }

    /**
     * 转换歧义词典的格式为新版本（去掉首列）
     */
    /**
     * @param input
     * @param output
     */
    public static void transFormatForAmbiguityDoc(String input, String output) {
        try {
            List<String> lines = getLines(input);
            if (lines == null) {
                log.info("输入文件【{}】异常", input);
                return;
            }
            PrintWriter writer = getPrintWriter(output);
            if (writer == null) {
                log.info("输出文件【{}】异常", output);
                return;
            }
            for (String line : lines) {
                int index = line.indexOf(FileUtil.TABS);
                if (index <= 0) {
                    continue;
                }
                writer.println(line.substring(index + 1, line.length()));
            }
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
            log.info("读取文件异常,输入文件路径:{}", input);
        }
    }

    /**
     * 递归获取指定目录下的所有的文件（包括子文件夹内的）.
     *
     * @param dirPath
     * @return
     */
    public static List<File> getAllFiles(String dirPath) {
        File dir = new File(dirPath);
        ArrayList<File> files = new ArrayList<File>();

        if (dir.isDirectory()) {
            File[] fileArr = dir.listFiles();
            for (int i = 0; i < fileArr.length; i++) {
                File f = fileArr[i];
                if (f.isFile()) {
                    files.add(f);
                } else {
                    files.addAll(getAllFiles(f.getPath()));
                }
            }
        }
        return files;
    }
}
